package com.yun.workqueue;

import com.rabbitmq.client.*;
import com.yun.utils.ConnectionUtil;

import java.io.IOException;

public class ConsumeA {

    public static void main(String[] arg) throws IOException {

        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare("work",false,false,false,null);
        //一次只给消费者发送一条信息
        channel.basicQos(1);

        /**
         * 参数1：work-队列名称
         * 参数2：消息是否自动确认
         */
        channel.basicConsume("work",false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("msg 1 = "+ new String(body));
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });
    }
}
